package org.Iteracion2.presentacion;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import javax.swing.JCheckBox;
import javax.swing.text.MaskFormatter;

import org.Iteracion2.dominio.Cliente;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.text.ParseException;

public class Nuevo_Cliente {

	public JFrame VentanaNuevo;
	private JPanel panel;
	private JButton button;
	private JLabel lblNombre;
	private JLabel lblApellidos;
	private JTextField txtNombre;
	private JTextField txtApellido;
	private JFormattedTextField ftxtDNI;
	private JLabel lblDni;
	private JButton btnGuardar;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Nuevo_Cliente window = new Nuevo_Cliente();
					window.VentanaNuevo.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Nuevo_Cliente() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		VentanaNuevo = new JFrame();
		VentanaNuevo.setTitle("A\u00F1adir cliente");
		VentanaNuevo.setVisible(true);
		VentanaNuevo.setBounds(100, 100, 439, 155);
		VentanaNuevo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		{
			panel = new JPanel();
			VentanaNuevo.getContentPane().add(panel, BorderLayout.CENTER);
			panel.setLayout(null);
			{
				button = new JButton("Atr\u00E1s");
				button.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						//Menu.VentanaMenu.setVisible(true);
						VentanaNuevo.setVisible(false);
					}
				});
				button.setBounds(311, 74, 84, 25);
				panel.add(button);
			}
			{
				lblNombre = new JLabel("Nombre:");
				lblNombre.setBounds(12, 13, 56, 16);
				panel.add(lblNombre);

			}
			{
				lblApellidos = new JLabel("Apellidos:");
				lblApellidos.setBounds(12, 42, 56, 16);
				panel.add(lblApellidos);
			}
			{
				txtNombre = new JTextField();
				txtNombre.addFocusListener(new FocusAdapter() {
					@Override
					public void focusGained(FocusEvent arg0) {
					}
				});
				txtNombre.setBounds(78, 10, 166, 22);
				panel.add(txtNombre);
				txtNombre.setColumns(10);
				txtNombre.addFocusListener(new MiFocusListener());
			}
			{
				txtApellido = new JTextField();
				txtApellido.setBounds(69, 39, 339, 22);
				panel.add(txtApellido);
				txtApellido.setColumns(10);
				txtApellido.addFocusListener(new MiFocusListener());
			}
			{
				MaskFormatter formatoDNI;
				try {
					formatoDNI = new MaskFormatter("########'-U");
					formatoDNI.setPlaceholderCharacter('X');
					ftxtDNI = new JFormattedTextField(formatoDNI);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				ftxtDNI.setBounds(298, 7, 110, 22);
				panel.add(ftxtDNI);
				ftxtDNI.addFocusListener(new MiFocusListener());
			}
			{
				lblDni = new JLabel("DNI:");
				lblDni.setBounds(268, 10, 30, 16);
				panel.add(lblDni);
			}
			{
				btnGuardar = new JButton("Guardar");
				btnGuardar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Cliente c;
						try {
							c = new Cliente(txtNombre.getText(), txtApellido.getText(),ftxtDNI.getText());
							if (c.insertar() == 1) {
								JOptionPane.showMessageDialog(VentanaNuevo,
										"Cliente guardado correctamente.","Usuario añadido",JOptionPane.INFORMATION_MESSAGE);
								VentanaNuevo.setVisible(false);
								//Menu.VentanaMenu.setVisible(true);
							} else {
								JOptionPane.showMessageDialog(VentanaNuevo,
										"Cliente no guardado correctamente.Vuelva a intentarlo.","Aviso",JOptionPane.ERROR_MESSAGE);

							}
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
				btnGuardar.setBounds(58, 74, 84, 25);
				panel.add(btnGuardar);
			}
		}
	}

	private class MiFocusListener extends FocusAdapter {
		@Override
		public void focusGained(FocusEvent e) {
			e.getComponent().setBackground(new Color(250, 250, 210));
		}

		@Override
		public void focusLost(FocusEvent e) {
			e.getComponent().setBackground(new Color(250, 250, 250));
		}
	}
}
